package com.ruoyi.web.controller.user;

import java.util.List;

import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.domain.entity.SysMember;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.SysGroup;
import com.ruoyi.system.domain.SysVgroup;
import com.ruoyi.system.service.ISysGroupService;
import com.ruoyi.system.service.ISysVgroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.service.ISysMemberService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-08-27
 */
@Controller
@RequestMapping("/system/member")
public class SysMemberController extends BaseController
{
    @Autowired
    private ServerConfig serverConfig;
    private String prefix = "system/member";

    @Autowired
    private ISysMemberService sysMemberService;
    @Autowired
    private ISysGroupService iSysGroupService;
    @Autowired
    private ISysVgroupService iSysVgroupService;
    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:member:view")
    @GetMapping()
    public String member()
    {
        return prefix + "/member";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMember sysMember)
    {
        startPage();
        List<SysMember> list = sysMemberService.selectSysMemberList(sysMember);
        for (int i=0;i<list.size();i++)
        {
            if(!StringUtils.ishttp(list.get(i).getPhoto()))
                list.get(i).setPhoto(serverConfig.getUrl()+list.get(i).getPhoto());
        }
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:member:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMember sysMember)
    {
        List<SysMember> list = sysMemberService.selectSysMemberList(sysMember);
        ExcelUtil<SysMember> util = new ExcelUtil<SysMember>(SysMember.class);
        return util.exportExcel(list, "用户数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        SysGroup sysGroup = new SysGroup();
        sysGroup.setState(0);
        SysVgroup sysVgroup = new SysVgroup();
        sysVgroup.setState(1);
        modelMap.put("group",iSysGroupService.selectSysGroupList(sysGroup));
        modelMap.put("vgroup",iSysVgroupService.selectSysVgroupList(sysVgroup));
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:member:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMember user)
    {
        if (StringUtils.isNotEmpty(user.getUsername()) && !sysMemberService.checkPhoneUnique_1(user))
        {
            return error("新增用户'" + user.getUsername() + "'失败，手机号码已存在");
        }
        user.setPhone(user.getUsername());
        user.setIinvicode(IdUtils.generateRandomCode(6));
        //昵称
        //user.setNickname(user.getUsername());
        user.setRegip(ShiroUtils.getIp());
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getUsername(), user.getPassword(), user.getSalt()));
        user.setRegdate(DateUtils.getNowDate());
        user.setInsertdate(DateUtils.getNowDate());

        return toAjax(sysMemberService.insertSysMember(user));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:member:edit")
    @GetMapping("/edit/{userid}")
    public String edit(@PathVariable("userid") Long userid, ModelMap mmap)
    {
        SysMember sysMember = sysMemberService.selectSysMemberByUserid(userid);
        SysGroup sysGroup = new SysGroup();
        sysGroup.setState(0);
        SysVgroup sysVgroup = new SysVgroup();
        sysVgroup.setState(1);
        mmap.put("group",iSysGroupService.selectSysGroupList(sysGroup));
        mmap.put("vgroup",iSysVgroupService.selectSysVgroupList(sysVgroup));
        mmap.put("sysmember", sysMember);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:member:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMember user)
    {
        if (StringUtils.isNotEmpty(user.getUsername()) && !sysMemberService.checkPhoneUnique_1(user))
        {
            return error("修改用户'" + user.getUsername() + "'失败，手机号码已存在");
        }
        if(!StringUtils.isEmpty(user.getPassword()) || StringUtils.isNotNull(user.getPassword())) {
            user.setSalt(ShiroUtils.randomSalt());
            user.setPassword(passwordService.encryptPassword(user.getUsername(), user.getPassword(), user.getSalt()));
        }
        return toAjax(sysMemberService.updateSysMember(user));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:member:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMemberService.deleteSysMemberByUserids(ids));
    }
    //验证手机号
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public boolean checkPhoneUnique(SysMember sysMember)
    {
        return sysMemberService.checkPhoneUnique_1(sysMember);
    }
}
